package com.googlecode.vanaraha.eclipse
import org.eclipse.jface.resource.ImageDescriptor
import org.eclipse.ui.plugin.AbstractUIPlugin
import org.osgi.framework.BundleContext
import com.googlecode.vanaraha.utils.Utils.logger._
import org.eclipse.jface.preference.IPreferenceStore
import com.googlecode.vanaraha.tool.DuplicateFinderTrait   

class Activator extends AbstractUIPlugin{
  
	override def start(context: BundleContext){
	  super.start(context)
	  Activator.plugin = this
	  debug("Activator#start has been called")
	}
  
  override def stop(context: BundleContext){
     super.stop(context)
     Activator.plugin = null
     debug("Activator#stop called.")
  }
    
}

object Activator {
  val PLUGIN_ID = "DuplicateFinderPlugin-Vanaraha"
  object constant{
	val toolToFindDuplicates = "com.googlecode.vanaraha.eclipse.tool.to.find.duplicate"
	val fileExtension = "com.googlecode.vanaraha.eclipse.file.extension"
	val limitMarkerAddition = "com.googlecode.vanaraha.eclipse.limit.marker.addition"
	val donotMarkRangeOfLines = "com.googlecode.vanaraha.eclipse.donot.mark.range.of.lines"
	val markerId = "com.googlecode.vanaraha.eclipse.marker"
    object default{
		val toolToFindDuplicates = "com.googlecode.vanaraha.adapter.simian.SimianDuplicateTool"
		val fileExtension = "java"
		val limitMarkerAddition = false
		val donotMarkRangeOfLines = false
    }
  }
  
  private var plugin: Activator = _
  def getImageDescriptor(path: String) = AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path)
  def getDefault = plugin
  def preferences = plugin.getPreferenceStore
  val vanarahaPrefs = new VanarahaPreferences
  def duplicateFinderTool = Class.forName(vanarahaPrefs.toolToFindDuplicates).newInstance.asInstanceOf[DuplicateFinderTrait]
  val fileTypePredicate: (String => Boolean) =  _.endsWith(vanarahaPrefs.fileExtension)
}

class VanarahaPreferences{
   import Activator._
   
   def toolToFindDuplicates = preferences.getString(constant.toolToFindDuplicates)
   def toolToFindDuplicates_=(tool: String) = preferences.setValue(constant.toolToFindDuplicates, tool)
   
   def fileExtension = preferences.getString(constant.fileExtension)
   def fileExtension_=(ext: String) =  preferences.setValue(constant.fileExtension, ext)
   
   def limitMarkerAddition = preferences.getBoolean(constant.limitMarkerAddition)
   def limitMarkerAddition_=(bool: Boolean) = preferences.setValue(constant.limitMarkerAddition, bool)
   
   def donotMarkRangeOfLines = preferences.getBoolean(constant.donotMarkRangeOfLines)
   def donotMarkRangeOfLines_=(bool: Boolean) = preferences.setValue(constant.donotMarkRangeOfLines, bool)
}